home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / text / words3.lha / Words / WarpSPELL / source / lib.c < prev    next >
C/C++ Source or Header  |  1995-06-20  |  5KB  |  182 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "defs.h"
  15.  
  16. Prototype LibCall struct Library *LibInit   (__D0 BPTR);
  17. Prototype LibCall struct Library *LibOpen   (__D0 long, __A0 struct Library *);
  18. Prototype LibCall long            LibClose  (__A0 struct Library *);
  19. Prototype LibCall long            LibExpunge(__A0 struct Library *);
  20.  
  21. struct Library *LibBase       = NULL;
  22. struct Library *RexxSysBase   = NULL;
  23. struct Library *DOSBase       = NULL;
  24. struct Library *IntuitionBase = NULL;
  25.  
  26. LONG SysBase = NULL;
  27. BPTR SegList = 0;
  28.  
  29. /*
  30.  *    The Initialization routine is given only a seglist pointer.  Since
  31.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  32.  *    and return either NULL or the library pointer.  Exec has Forbid()
  33.  *    for us during the call.
  34.  *
  35.  *    We use an extended library structure to allow identification as a
  36.  *    GoldED syntax scanner.
  37.  */
  38.  
  39. LibCall struct Library *
  40. LibInit(__D0 BPTR segment)
  41. {
  42.     struct Library *lib;
  43.  
  44.     static const long Vectors[] = {
  45.  
  46.         (long)ALibOpen,
  47.         (long)ALibClose,
  48.         (long)ALibExpunge,
  49.         (long)ALibReserved,
  50.  
  51.         (long)MountScanner,
  52.         (long)StartScanner,
  53.         (long)CloseScanner,
  54.         (long)FlushScanner,
  55.         (long)SetupScanner,
  56.         (long)BriefScanner,
  57.         (long)ParseLine,
  58.         (long)UnparseLines,
  59.         (long)ParseSection,
  60.         -1
  61.     };
  62.  
  63.     SysBase = *(long *)4;
  64.  
  65.     DOSBase       = OpenLibrary("dos.library", 0);
  66.     RexxSysBase   = OpenLibrary("rexxsyslib.library", 36);
  67.     IntuitionBase = OpenLibrary("intuition.library",  36);
  68.  
  69.     if (DOSBase && RexxSysBase && IntuitionBase) {
  70.  
  71.         if (LibBase = lib = MakeLibrary((APTR)Vectors, NULL, NULL, sizeof(struct ParserBase), NULL)) {
  72.  
  73.             lib->lib_Node.ln_Type = NT_LIBRARY;
  74.             lib->lib_Node.ln_Name = LibName;
  75.             lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  76.             lib->lib_Version      = 37;
  77.             lib->lib_Revision     = 0;
  78.             lib->lib_IdString     = (APTR)LibId;
  79.  
  80.             ((struct ParserBase *)lib)->Magic = PARSER_MAGIC;
  81.  
  82.             SegList = segment;
  83.  
  84.             AddLibrary(lib);
  85.  
  86.             InitC();
  87.  
  88.             return(lib);
  89.         }
  90.     }
  91.  
  92.     return(NULL);
  93. }
  94.  
  95. /*
  96.  *    Open is given the library pointer and the version request.  Either
  97.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  98.  *    Exec has Forbid() for us during the call.
  99.  */
  100.  
  101. LibCall struct Library *
  102. LibOpen(__D0 long version, __A0 struct Library *lib)
  103. {
  104.     ++lib->lib_OpenCnt;
  105.  
  106.     lib->lib_Flags &= ~LIBF_DELEXP;
  107.  
  108.     return(lib);
  109. }
  110.  
  111. /*
  112.  *    Close is given the library pointer and the version request.  Be sure
  113.  *    not to decrement the open count if already zero.  If the open count
  114.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  115.  *    and return the seglist.  Otherwise we return NULL.
  116.  *
  117.  *    Note that this routine never sets LIBF_DELEXP on its own.
  118.  *
  119.  *    Exec has Forbid() for us during the call.
  120.  */
  121.  
  122. LibCall long
  123. LibClose(__A0 struct Library *lib)
  124. {
  125.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  126.         return(NULL);
  127.  
  128.     if (lib->lib_Flags & LIBF_DELEXP)
  129.         return(LibExpunge(lib));
  130.  
  131.     return(NULL);
  132. }
  133.  
  134. /*
  135.  *    We expunge the library and return the Seglist ONLY if the open count
  136.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  137.  *    flag and return NULL.
  138.  *
  139.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  140.  *    might be called from the memory allocator and thus we CANNOT DO A
  141.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  142.  *
  143.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  144.  *    therefore freeze if we called it ourselves.  As far as I can tell
  145.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  146.  *    below.
  147.  */
  148.  
  149. LibCall long
  150. LibExpunge(__A0 struct Library *lib)
  151. {
  152.     if (lib->lib_OpenCnt) {
  153.  
  154.         lib->lib_Flags |= LIBF_DELEXP;
  155.         return(NULL);
  156.     }
  157.  
  158.     Remove(&lib->lib_Node);
  159.  
  160.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  161.  
  162.     if (IntuitionBase) {
  163.  
  164.         CloseLibrary((struct Library *)IntuitionBase);
  165.         IntuitionBase = NULL;
  166.     }
  167.  
  168.     if (RexxSysBase) {
  169.  
  170.         CloseLibrary((struct Library *)RexxSysBase);
  171.         RexxSysBase = NULL;
  172.     }
  173.  
  174.     if (DOSBase) {
  175.  
  176.         CloseLibrary((struct Library *)DOSBase);
  177.         DOSBase = NULL;
  178.     }
  179.  
  180.     return((long)SegList);
  181. }
  182.